//
//  RepairHistoryVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/22.
//  Copyright © 2020 tym. All rights reserved.
//

#import "RepairHistoryVC.h"
#import "GroupShadowTableView.h"
#import "OrderHistoryCell.h"
#import "Sizes.h"
#import "RepairShowVC.h"
#import "RepairDetailVC.h"
#import "EquipmentService.h"
@interface RepairHistoryVC ()<GroupShadowTableViewDelegate,GroupShadowTableViewDataSource>

@property(nonatomic,strong) GroupShadowTableView *tableView;

@property(nonatomic,strong) NSMutableArray *dataArray;

@property(nonatomic,strong) EquipmentService *services;
@end

@implementation RepairHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTable];
}

-(void)zj_viewWillAppearForIndex:(NSInteger)index{
    [self getRepairList];
}

-(EquipmentService*)services{
    if (_services == nil) {
        _services = [[EquipmentService alloc] init];
    }
    return _services;
}

-(void)getRepairList{
    TYMWeak(self);
    [self.services getFindequipmentrepairsapplyFamilyID:[UserTool getCurrentFamily].familyId page:1 size:100 completion:^(LMJBaseResponse * _Nonnull respons, NSArray<RepairsApplyModel *> * _Nonnull repair) {
        if (weakself.dataArray.count) {
            [weakself.dataArray removeAllObjects];
        }
        [weakself.dataArray addObjectsFromArray:repair];
        [weakself.tableView reloadData];
    }];
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavigateonHeight-44-SafeBottomMargin -10) style:UITableViewStyleGrouped];
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.groupShadowDelegate = self;
    _tableView.groupShadowDataSource = self;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = viewBGColor;
    [_tableView registerClass:[OrderHistoryCell class] forCellReuseIdentifier:[OrderHistoryCell identifier]];
    [self.view addSubview:_tableView];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)]; view.backgroundColor = [UIColor redColor]; self.tableView.tableHeaderView = view;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [OrderHistoryCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OrderHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:[OrderHistoryCell identifier]];
    if (self.dataArray.count) {
        cell.repaireModel = self.dataArray[indexPath.section];
    }
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataArray.count) {
        RepairShowVC  *repairVC = [[RepairShowVC alloc] initWithShow:self.dataArray[indexPath.section]];
        [self.navigationController pushViewController:repairVC animated:YES];
    }
}

@end
